package com.jnc.plug.mybatis.mp.generator.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.mysql.jdbc.Driver;
import org.apache.commons.lang3.StringUtils;

import java.util.Properties;

/**
 * @Description
 * @Author fanlei
 * @Version 1.0
 * @Date 2019/9/28
 */
public class Context {
    /*****************全局配置********************/
    //global.output.dir
    public volatile static String outputDir = "C:\\JNC-MP";
    //global.author
    public volatile static String author = "JNC";
    //global.base.result.map
    public volatile static Boolean baseResultMap = true;
    //global.swagger2
    public volatile static Boolean swagger2 = false;
    //global.entity.name
    public volatile static String entityName = "%sEntity";
    //global.controller.name
    public volatile static String controllerName = "%sController";
    //global.service.name
    public volatile static String serviceName = "%sService";
    //global.service.impl.name
    public volatile static String serviceImplName = "%sServiceImpl";
    //global.mapper.name
    public volatile static String mapperName = "%sMapper";
    //global.xml.name
    public volatile static String xmlName = "%sXml";
    /*****************全局配置********************/

    /*****************数据源配置********************/
    //dataSource.db.type
    public volatile static DbType dbType = DbType.MYSQL;
    //dataSource.driver.name
    public volatile static String driverName = Driver.class.getName();
    //dataSource.username
    public volatile static String username = "root";
    //dataSource.password
    public volatile static String password = "123456";
    //dataSource.url
    public volatile static String url = "jdbc:mysql://127.0.0.1:3306/mp_test?useUnicode=true&allowPublicKeyRetrieval=true&useSSL=false&characterEncoding=utf8";
    /*****************数据源配置********************/

    /*****************策略配置********************/
    //"bmd_", "mp_", "tm"
    //strategy.table.prefixs
    public volatile static String[] tablePrefix = {};
    //strategy.naming
    public volatile static NamingStrategy naming = NamingStrategy.underline_to_camel;
    //strategy.includes
    public volatile static String[] include = {};
    //strategy.excludes
    public volatile static String[] exclude = {};
    //Class.forName("com.baomidou.demo.TestEntity")
    //strategy.super.entity.class
    public volatile static String superEntityClass = "";
    //strategy.super.entity.columns
    public volatile static String[] superEntityColumns = {};
    //strategy.entity.column.removeis.prefix
    public volatile static Boolean entityBooleanColumnRemoveIsPrefixtrue = true;
    //strategy.rest.controller.style
    public volatile static Boolean restControllerStyle = true;
    //strategy.entity.lombok.model
    public static boolean entityLombokModel = false;
    /*****************策略配置********************/

    /*****************包配置********************/
    //package.parent
    public volatile static String parent = "";
    //package.module.name
    public volatile static String moduleName = "";
    //package.entity
    public volatile static String entity = "entity";
    //package.controller
    public volatile static String controller = "controller";
    //package.service.impl
    public volatile static String serviceImpl = "service.impl";
    //package.service
    public volatile static String service = "service";
    //package.mapper
    public volatile static String mapper = "mapper";
    //package.xml
    public volatile static String xml = "mapper.xml";

    /*****************包配置********************/

    /*****************模板配置********************/
    //template.controller
    public static String templateController = "/jnc-template/controller.java.vm";
    //template.entity
    public static String templateEntity = "/jnc-template/entity.java.vm";
    //template.mapper
    public static String templateMapper = "/jnc-template/mapper.java.vm";
    //template.mapper.xml
    public static String templateXml = "/jnc-template/mapper.xml.vm";
    //template.service
    public static String templateService = "/jnc-template/service.java.vm";
    //template.service.impl
    public static String templateServiceImpl = "/jnc-template/serviceImpl.java.vm";
    /*****************模板配置********************/

    public static void setConfig(Properties properties){
        outputDir = properties.getProperty("global.output.dir", outputDir);
        author = properties.getProperty("global.author", author);
        baseResultMap = booleanConvert(properties.getOrDefault("global.base.result.map", baseResultMap));
        swagger2 = booleanConvert(properties.getOrDefault("global.swagger2", swagger2));
        entityName = properties.getProperty("global.entity.name", entityName);
        controllerName = properties.getProperty("global.controller.name", controllerName);
        serviceName = properties.getProperty("global.service.name", serviceName);
        serviceImplName = properties.getProperty("global.service.impl.name", serviceImplName);
        mapperName = properties.getProperty("global.mapper.name", mapperName);
        xmlName = properties.getProperty("global.xml.name", xmlName);


        dbType = DbType.getDbType(properties.getProperty("dataSource.db.type", DbType.MYSQL.getDb()));
        driverName = properties.getProperty("dataSource.driver.name", driverName);
        username = properties.getProperty("dataSource.username", username);
        password = properties.getProperty("dataSource.password", password);
        url = properties.getProperty("dataSource.url", url);


        tablePrefix = splitStr(properties.getProperty("strategy.table.prefixs"));
        naming = getNamingStrategy(properties.getProperty("strategy.naming"));
        include = splitStr(properties.getProperty("strategy.includes"));
        exclude = splitStr(properties.getProperty("strategy.excludes"));
        superEntityClass = properties.getProperty("strategy.super.entity.class", superEntityClass);
        superEntityColumns = splitStr(properties.getProperty("strategy.super.entity.columns"));
        entityBooleanColumnRemoveIsPrefixtrue = booleanConvert(properties.getOrDefault("strategy.entity.column.removeis.prefix", entityBooleanColumnRemoveIsPrefixtrue));
        restControllerStyle = booleanConvert(properties.getOrDefault("strategy.rest.controller.style", restControllerStyle));
        restControllerStyle = booleanConvert(properties.getOrDefault("strategy.rest.controller.style", restControllerStyle));
        entityLombokModel = booleanConvert(properties.getOrDefault("strategy.entity.lombok.model", entityLombokModel));

        parent = properties.getProperty("package.parent", parent);
        moduleName = properties.getProperty("package.module.name", moduleName);
        entity = properties.getProperty("package.entity", entity);
        controller = properties.getProperty("package.controller", controller);
        service = properties.getProperty("package.service", service);
        serviceImpl = properties.getProperty("package.service.impl", serviceImpl);
        mapper = properties.getProperty("package.mapper", mapper);
        xml = properties.getProperty("package.xml", xml);


        templateController = properties.getProperty("template.controller", templateController);
        templateEntity = properties.getProperty("template.entity", templateEntity);
        templateMapper = properties.getProperty("template.mapper", templateMapper);
        templateXml = properties.getProperty("template.mapper.xml", templateXml);
        templateService = properties.getProperty("template.service", templateService);
        templateServiceImpl = properties.getProperty("template.service.impl", templateServiceImpl);

    }

    private static Boolean booleanConvert(Object orDefault) {
        return Boolean.valueOf(String.valueOf(orDefault));
    }

    private static String[] splitStr(String str) {
        if (!StringUtils.isBlank(str)){
            return str.split(",");
        }else {
            return new String[]{};
        }
    }

    public static NamingStrategy getNamingStrategy(String naming) {
        if (NamingStrategy.no_change.name().equalsIgnoreCase(naming)){
            return NamingStrategy.no_change;
        }else {
            return NamingStrategy.underline_to_camel;
        }
    }
}
